home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: binary tree question
- Date: 22 Mar 1996 09:05:23 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4iumkjINNsp3@keats.ugrad.cs.ubc.ca>
- References: <4isglj$cgg@netaxs.com>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4isglj$cgg@netaxs.com>, J. A. McNamara <grulm@netaxs.com> wrote:
- >I'm willing to bet that the answer to this is obvious, but I'm trying to
- >free each node of a binary tree as it is read -- preferably *after* it's
- >been read :) the following code doesn't appear to free the memory, but I
- >haven't figured out how to prod my debugger into telling me how much memory
- >is being used, so I'm not so sure . . . (by the way, it gives me no
- >problems in any other respect). (I'd also like to mention that this is
- >*not* homework)
- >
- >/***********************************************************************/
- >/*link is the linked list struct type; tree_n is the tree node struct type*/
- >/* addlink() works fine, too, and both are prototyped. */
- >
- >link *inorder(tree_n *tn, link *tail, int *i) /* read tree into list */
- > {
- > if (tn != NULL) {
- > tail = inorder(tn->l, tail, i);
- > tail = addlink(tail, tn->f); /* hand data ptr to list */
- > (*i)++; /* keep track of total */
- > tail = inorder(tn->r, tail, i);
- > }
- > if (tn!=NULL) free(tn); tn=NULL;
- > return tail;
- > }
- >/**************************************************************************/
-
- This is not a pure ``inorder'' traversal. The nodes are being added to the
- linked list in order, but you are freeing in post-order (children are visited
- first, then you free the parent).
-
- Why don't you combine the two if() statements into one? They have precisely the
- same expression, and tn is never assigned to in the body of the first one, so
- the evaluation of the expression doesn't change.
-
- You do not need to assign NULL to the ``tn'' pointer after you have invoked
- free() on it. This is a waste of time, since it has been passed to the
- procedure by value. You are only affecting the value of the function parameter
- ``tn'', not any external object. This value is never used, so the compiler will
- throw that assignment away at the optimization phase.
-
- >I also tried the following:
- >
- >free(tn->l); tn->l=NULL;
- >free(tn->r); tn->r=NULL;
- >
- >. . . which gave me some serious garbage, though I'm not quite sure why.
-
- Because you probably did not check whether the children are null pointers or
- not, only whether tn is a null pointer. Just because tn is a valid tree node
- doesn't mean that tn->l or tn->r are. The above modification will also fail to
- free the root node.
-
- >any help would be appreciated. thanks in advance.
-
- Your code appears fine. That it's not actually freeing memory is not
- surprising. Many free() implementations don't free memory, they only return
- free blocks to a list from where subsequent malloc() invocations can obtain
- memory quickly.
-
- Exception: on some UNIX systems, there is a special malloc() implementation
- which will use what are called memory mapped regions for very large blocks
- (like if you malloc ten megabytes, say), and these *are*, fortunately, returned
- to the system when you call free()!
- --
-
-